Fire up GraphLab Create

We always start with this line before using any part of GraphLab Create


In [1]:
import graphlab

Load a tabular data set


In [3]:
sf = graphlab.SFrame('people-example.csv')


Finished parsing file /Users/zhenhua.xu/Documents/Coursera-Machine-Learning-Specialization/people-example.csv
Parsing completed. Parsed 7 lines in 0.021729 secs.
------------------------------------------------------
Inferred types from first 100 line(s) of file as 
column_type_hints=[str,str,str,int]
If parsing fails due to incorrect types, you can correct
the inferred type list above and pass it to read_csv in
the column_type_hints argument
------------------------------------------------------
Finished parsing file /Users/zhenhua.xu/Documents/Coursera-Machine-Learning-Specialization/people-example.csv
Parsing completed. Parsed 7 lines in 0.009924 secs.

SFrame basics


In [4]:
sf #we can view first few lines of table


Out[4]:
First Name Last Name Country age
Bob Smith United States 24
Alice Williams Canada 23
Malcolm Jone England 22
Felix Brown USA 23
Alex Cooper Poland 23
Tod Campbell United States 22
Derek Ward Switzerland 25
[7 rows x 4 columns]

In [5]:
sf.tail()  # view end of the table


Out[5]:
First Name Last Name Country age
Bob Smith United States 24
Alice Williams Canada 23
Malcolm Jone England 22
Felix Brown USA 23
Alex Cooper Poland 23
Tod Campbell United States 22
Derek Ward Switzerland 25
[7 rows x 4 columns]

GraphLab Canvas


In [7]:
# .show() visualizes any data structure in GraphLab Create
sf.show()


Canvas is accessible via web browser at the URL: http://localhost:59966/index.html
Opening Canvas in default web browser.

In [8]:
# If you want Canvas visualization to show up on this notebook, 
# rather than popping up a new window, add this line:
graphlab.canvas.set_target('ipynb')

In [10]:
sf['age'].show(view='Categorical')


Inspect columns of dataset


In [11]:
sf['Country']


Out[11]:
dtype: str
Rows: 7
['United States', 'Canada', 'England', 'USA', 'Poland', 'United States', 'Switzerland']

In [12]:
sf['age']


Out[12]:
dtype: int
Rows: 7
[24, 23, 22, 23, 23, 22, 25]

Some simple columnar operations


In [13]:
sf['age'].mean()


Out[13]:
23.142857142857146

In [14]:
sf['age'].max()


Out[14]:
25

Create new columns in our SFrame


In [15]:
sf


Out[15]:
First Name Last Name Country age
Bob Smith United States 24
Alice Williams Canada 23
Malcolm Jone England 22
Felix Brown USA 23
Alex Cooper Poland 23
Tod Campbell United States 22
Derek Ward Switzerland 25
[7 rows x 4 columns]

In [16]:
sf['Full Name'] = sf['First Name'] + ' ' + sf['Last Name']

In [17]:
sf


Out[17]:
First Name Last Name Country age Full Name
Bob Smith United States 24 Bob Smith
Alice Williams Canada 23 Alice Williams
Malcolm Jone England 22 Malcolm Jone
Felix Brown USA 23 Felix Brown
Alex Cooper Poland 23 Alex Cooper
Tod Campbell United States 22 Tod Campbell
Derek Ward Switzerland 25 Derek Ward
[7 rows x 5 columns]

In [18]:
sf['age'] * sf['age']


Out[18]:
dtype: int
Rows: 7
[576, 529, 484, 529, 529, 484, 625]

Use the apply function to do a advance transformation of our data


In [19]:
sf['Country']


Out[19]:
dtype: str
Rows: 7
['United States', 'Canada', 'England', 'USA', 'Poland', 'United States', 'Switzerland']

In [20]:
sf['Country'].show()



In [21]:
def transform_country(country):
    if country == 'USA':
        return 'United States'
    else:
        return country

In [22]:
transform_country('Brazil')


Out[22]:
'Brazil'

In [23]:
transform_country('Brasil')


Out[23]:
'Brasil'

In [24]:
transform_country('USA')


Out[24]:
'United States'

In [25]:
sf['Country'].apply(transform_country)


Out[25]:
dtype: str
Rows: 7
['United States', 'Canada', 'England', 'United States', 'Poland', 'United States', 'Switzerland']

In [26]:
sf['Country'] = sf['Country'].apply(transform_country)

In [27]:
sf


Out[27]:
First Name Last Name Country age Full Name
Bob Smith United States 24 Bob Smith
Alice Williams Canada 23 Alice Williams
Malcolm Jone England 22 Malcolm Jone
Felix Brown United States 23 Felix Brown
Alex Cooper Poland 23 Alex Cooper
Tod Campbell United States 22 Tod Campbell
Derek Ward Switzerland 25 Derek Ward
[7 rows x 5 columns]

New Cell


In [ ]: